package com.iflytek.autoCompareTool.service;

import com.iflytek.autoCompareTool.common.Config;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.*;

/**
 * @author llchen12
 * @date 2018/6/8
 */
public class Executor {

    private ExecutorService processThreadPool;

    private ExecutorService workThreadPool;

    private ScheduledExecutorService monitorScheduler;

    private Executor() {
        init();
    }

    public static Executor instance;

    public static Executor getInstance() {
        if (instance == null) {
            synchronized (Executor.class) {
                if (instance == null) {
                    instance = new Executor();
                }
            }
        }
        return instance;
    }

    public ExecutorService getProcessThreadPool() {
        return processThreadPool;
    }

    public ExecutorService getWorkThreadPool() {
        return workThreadPool;
    }

    public ScheduledExecutorService getMonitorScheduler() {
        return monitorScheduler;
    }

    private void init() {
        ThreadFactory processThreadFactory = new BasicThreadFactory.Builder()
                .namingPattern("process-thread-pool-%d")
                .daemon(false).build();

        ThreadFactory workThreadFactory = new BasicThreadFactory.Builder()
                .namingPattern("work-thread-pool-%d")
                .daemon(false).build();
        workThreadPool = new ThreadPoolExecutor(Config.WORKER_THREAD_CORESIZE,
                Config.WORKER_THREAD_MAXSIZE, 1, TimeUnit.MINUTES,
                new LinkedBlockingQueue<>(20000), workThreadFactory);
        processThreadPool = new ThreadPoolExecutor(Config.PROCESSOR_THREAD_CORESIZE,
                Config.PROCESSOR_THREAD_MAXSIZE, 5, TimeUnit.MINUTES,
                new LinkedBlockingQueue<>(1024), processThreadFactory);
        monitorScheduler = new ScheduledThreadPoolExecutor(1, new BasicThreadFactory.Builder()
                .namingPattern("monitor-schedule-pool-%d").daemon(true).build());
    }


}
